package com.zyk.scaffold.tcp.wrappers;

import com.zyk.scaffold.tcp.proto3.MessageBuf;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;

@Data
@Document("tcp_logs")
public class MessageWrapper implements Serializable {

    public static MessageWrapper loadJMTransfer(MessageBuf.JMTransfer jmTransfer){
        MessageWrapper messageWrapper = new MessageWrapper();
        messageWrapper.setVersion(jmTransfer.getVersion());
        messageWrapper.setDeviceId(jmTransfer.getDeviceId());
        messageWrapper.setEncryptionType(jmTransfer.getEncryptionTypeValue());
        messageWrapper.setPlatformType(jmTransfer.getPlatformTypeValue());
        messageWrapper.setGroupName(jmTransfer.getGroupName());
        messageWrapper.setName(jmTransfer.getName());
        messageWrapper.setAppId(jmTransfer.getAppId());
        messageWrapper.setTimeStamp(jmTransfer.getTimeStamp());
        messageWrapper.setSign(jmTransfer.getSign());
        messageWrapper.setMsgTypeValue(jmTransfer.getMsgTypeValue());
        messageWrapper.setCmd(jmTransfer.getCmd());
        messageWrapper.setSeq(jmTransfer.getSeq());
        messageWrapper.setBody(Body.loadBody(jmTransfer.getBody()));
        return messageWrapper;
    }

    /**
     * 接口版本号
     */
    private String version;

    /**
     * 设备uuid
     */
    private String deviceId;

    /**
     * 加密类型
     * NONE = 0;
     * RSA = 1;
     * AES = 2;
     */
    private Integer encryptionType;

    /**
     * 平台类型
     * IOS = 0;
     * ANDROID = 1;
     * WIN = 2;
     * MAC = 3;
     * DU = 4;
     */
    private Integer platformType;

    /**
     * 名称
     */
    private String name ;

    /**
     * 组名称
     */
    private String groupName ;

    /**
     * 客户端凭证
     */
    private String token;

    /**
     * 客户端key
     */
    private String appId;

    /**
     * 时间戳
     */
    private String timeStamp;

    /**
     * 签名 md5(secret++(body base64)++(timeStamp)++secret)
     */
    private String sign;

    /**
     * 消息类型
     * LOGIN = 0;//[客户端]登录
     * PUSH = 1;//[客户端]推送数据
     * NOTIFY = 2;//[服务端]通知
     * PULL = 3;//[客户端]拉取数据
     * HEARTBEAT = 4;//[客户端]心跳
     * PROXY = 5;//[客户端]代理
     * REPLY = 6;//[服务端、客户端]应答
     */
    private Integer msgTypeValue;

    /**
     * 请求接口命令
     */
    private Integer cmd;

    /**
     * 请求序列号
     */
    private Long seq;

    /**
     * 请求体
     */
    private Body body;

    public MessageBuf.JMTransfer toMessageBuf() {

        // TODO
        return MessageBuf.JMTransfer.newBuilder().build();
    }

    @Data
    public static class Body{
        private String msg;

        public static Body loadBody(MessageBuf.JMTransfer.Body body) {
            Body bodyTarget = new Body();
            bodyTarget.setMsg(body.getMsg());
            return bodyTarget;
        }
    }
}
